package com.danskin.wechat.webapi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.danskin.wechat.entity.BannerInfo;
import com.danskin.wechat.entity.ProductGroupon;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.BannerInfoDTO;
import com.danskin.wechat.model.ProductGrouponDTO;
import com.danskin.wechat.model.ProductGrouponDetailsDTO;
import com.danskin.wechat.service.BannerInfoService;
import com.danskin.wechat.service.ProductGrouponService;
import com.danskin.wechat.util.ConvertUtils;
import com.danskin.wechat.util.Page;

@RestController
@RequestMapping("api/productGroupon")
public class ProductGrouponApiController extends BaseApiController{
	@Resource
	private ProductGrouponService productGrouponService;
	@Resource
	private BannerInfoService bannerInfoService;
	/**
	 * 当月精选列表
	 * @param appId
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findProductGroupon/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object findProductGroupon(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map,HttpServletRequest request){
		if (!map.containsKey("type")) {
			return this.getError("分类不能为空");
		}else if (!map.containsKey("pageIndex")) {
			return this.getError("pageIndex不能为空");
		}
		String sType = map.get("type").toString();
		String sPageIndex= map.get("pageIndex").toString();
		int pageIndex = ConvertUtils.getInt(sPageIndex);
		int pageSize = 10;
		map.put("appId", appId);
		Page page = new Page();
		page.setCurrentResult(pageIndex);
		page.setShowCount(pageSize);
		page.setMap(map);
		List<ProductGroupon> productGrouponList = productGrouponService.findProductGrouponListAPI(page, sType);
		List<ProductGrouponDTO> listDto = new ArrayList<ProductGrouponDTO>();
		if(productGrouponList.size()>0){
			for(ProductGroupon groupon:productGrouponList){
				ProductGrouponDTO dto = new ProductGrouponDTO();
				dto.setId(String.valueOf(groupon.getId()));
				dto.setImgUrl(groupon.getDetailImg());
				dto.setTitle(groupon.getName());
				dto.setGroupPrice(String.valueOf(groupon.getSalePrice()));
				dto.setOriginalPrice(String.valueOf(groupon.getOriginPrice()));
				dto.setMark(this.getTagInfo().get(groupon.getTagInfo()));
				listDto.add(dto);
			}
		}
		return new AccessResponse<Object>("0", "ok", listDto);
	}
	/***
	 * 优选列表(当月精选)
	 * @param appId
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryProductGrouponIsHot/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object queryProductGrouponIsHot(@PathVariable("appId") String appId,HttpServletRequest request){
		List<ProductGroupon> productGrouponList = productGrouponService.queryProductGrouponListIsHot(appId);
		List<ProductGrouponDTO> listDto = new ArrayList<ProductGrouponDTO>();
		if(productGrouponList.size()>0){
			for(ProductGroupon groupon:productGrouponList){
				ProductGrouponDTO dto = new ProductGrouponDTO();
				dto.setId(String.valueOf(groupon.getId()));
				dto.setImgUrl(groupon.getDetailImg());
				dto.setTitle(groupon.getName());
				dto.setGroupPrice(String.valueOf(groupon.getSalePrice()));
				dto.setOriginalPrice(String.valueOf(groupon.getOriginPrice()));
				dto.setMark(this.getTagInfo().get(groupon.getTagInfo()));
				listDto.add(dto);
			}
		}
		return new AccessResponse<Object>("0", "ok", listDto);
	}
	/***
	 * 商品详情(当月精选)
	 * @param appId
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryProductGrouponDetails/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object queryProductGrouponDetails(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map,HttpServletRequest request){
		if (!map.containsKey("id")) {
			return this.getError("id不能为空");
		}
		String id = map.get("id").toString();
		ProductGroupon productGroupon = productGrouponService.findProductGrouponDetails(appId,id);
		ProductGrouponDetailsDTO dto=null;
		if(productGroupon!=null){
			 dto = new ProductGrouponDetailsDTO();
			 dto.setId(String.valueOf(productGroupon.getId()));
			 dto.setPropic(productGroupon.getIconImg());
			 dto.setNowPrice(String.valueOf(productGroupon.getSalePrice()));
			 dto.setOriginal(String.valueOf(productGroupon.getOriginPrice()));
			 dto.setProMsg(productGroupon.getName());
			 dto.setContains(productGroupon.getDetails());
			 dto.setO2oUrl(productGroupon.getO2o());
		}
		return new AccessResponse<Object>("0", "ok", dto);
	}
	/***
	 * 当月精选轮播图
	 * @param appId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryBannerInfoModth/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object queryBannerInfoModth(@PathVariable("appId") String appId,HttpServletRequest request){
		List<BannerInfo> list = bannerInfoService.findBannerInfoModth(appId);
		List<BannerInfoDTO> dtoList = new ArrayList<BannerInfoDTO>();
		if(list.size()>0){
			for(BannerInfo info:list){
				BannerInfoDTO dto = new BannerInfoDTO();
				dto.setImg(info.getPicUrl());
				dto.setUrl(info.getLinkUrl());
				dtoList.add(dto);
			}
		}
		return new AccessResponse<Object>("0", "ok", dtoList);
	}
	public Map<Integer,String> getTagInfo(){
		Map<Integer,String> map = new HashMap<Integer, String>();
		map.put(1, "爆款");
		map.put(2, "热卖");
		map.put(3, "新品");
		return map;
	}
}
